<?php

namespace App\Models\UserModels;

use App\Models\ParentModel;
use DB;

/**
 * 合伙人用户model
 * @author puppet
 */
class User extends ParentModel
{
    /**
     * 与模型关联的数据表。
     *
     * @var string
     */
    protected $table = 'pt_user';

    /**
     * DB::直接操作的数据库表名
     * @var string
     */
    protected $db_table = 'pt_user';

    /**
     * 主键 默认id。
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 指定是否模型应该被戳记时间。
     *
     * @var bool
     */
    public $timestamps = false;
    //查询用户的open以及对应的token
    /**
     * 翻页展示所有信息
     *	授权账号列表
     */
    //获取openid
    function getuserinfo(){
        $sql="select a.open_id,b.access_token,b.refresh_token from $this->table as a ";
        $sql.=" left join pt_dy_access_token as b on a.open_id=b.open_id  ";
        $infolist = DB::select($sql);
        return $infolist;

    }

    //查询授权用户信息及token
    function selectUser()
    {
        $sql = "select open_id from pt_dy_access_token where status=?";
        $re = DB::select($sql, [1]);
        return isset($re[0]) ? $re : false;
    }

    function findToken($open_id)
    {
        $sql = "select * from pt_dy_access_token where open_id=?";
        $re = DB::select($sql, [$open_id]);
        return isset($re[0]) ? $re[0] : false;
    }

    //更新抖音用户、达人信息
    function upDyUserInfo($arr, $open_id)
    {
        $e_account_role = "普通用户";
        $gender = '未知';
        if (isset($arr['e_account_role']) && $arr['e_account_role'] == 'EAccountM') {
            $e_account_role = "普通企业号";
        } elseif (isset($arr['e_account_role']) && $arr['e_account_role'] == 'EAccountS') {
            $e_account_role = "认证企业号";
        } elseif (isset($arr['e_account_role']) && $arr['e_account_role'] == 'EAccountK') {
            $e_account_role = "品牌企业号";
        }
        if (isset($arr['gender']) && $arr['gender'] == 1) {
            $gender = "男";
        } elseif (isset($arr['gender']) && $arr['gender'] == 2) {
            $gender = "女";
        }
        $dataOne = [
            'e_account_role' => $e_account_role,
            'gender' => $gender,
            'province' => isset($arr['province']) ? $arr['province'] : '',
            'union_id' => isset($arr['union_id']) ? $arr['union_id'] : '',
            'avatar' => isset($arr['avatar']) ? $arr['avatar'] : '',
            'nickname' => isset($arr['nickname']) ? htmlspecialchars($arr['nickname'], ENT_QUOTES) : '',
            'city' => isset($arr['city']) ? $arr['city'] : '',
            'country' => isset($arr['country']) ? $arr['country'] : '',
            'update_time' => date('Y-m-d H:i:s', time()),
        ];
        $dataTwo = [
            'tl_uname' => isset($arr['nickname']) ? htmlspecialchars($arr['nickname'], ENT_QUOTES) : '',
            'tl_uimg' => isset($arr['avatar']) ? $arr['avatar'] : '',
            'tl_u_type' => $e_account_role,
            'gender' => $gender,
        ];

        $sqlOne = "update pt_user set ";
        foreach ($dataOne as $k => $v) {
            $sqlOne .= "`{$k}`='{$v}',";
        }
        $sqlOne = substr($sqlOne, 0, -1) . " where open_id='{$open_id}'";

        $sqlTwo = "update pt_talent set ";
        foreach ($dataTwo as $k => $v) {
            $sqlTwo .= "`{$k}`='{$v}',";
        }
        $sqlTwo = substr($sqlTwo, 0, -1) . " where open_id='{$open_id}'";

        DB::update($sqlOne);
        DB::update($sqlTwo);
    }

    //用户列表展示ajax（会员抖音授权账户）
    function selectUserList($params)
    {
        $res = array();
        $size = (isset($params['limit']) && intval($params['limit']) > 0 && intval($params['limit']) <= 100) ? $params['limit'] : 10;
        $page = isset($params['page']) ? intval($params['page']) : 1;
        $offset = ($page - 1) * $size;
        $nickname = isset($params['nickname']) ? $params['nickname'] : '';

        $countSql = "select count(DISTINCT open_id) as count from {$this->table} where 1=?";
        $sql = "select * from {$this->table} where 1=?";

        if ($nickname) {
            $countSql .= " and nickname like '%{$nickname}%'";
            $sql .= " and nickname like '%{$nickname}%'";
        }

        $sql .= " group by open_id limit {$offset},{$size}";

        $count = DB::select($countSql, [1]);
        $count = isset($count[0]['count']) ? $count[0]['count'] : 0;
        $data = DB::select($sql, [1]);

        if ($data) {
            $i = 0;
            foreach ($data as $k => $v) {
                $data[$k]['kid'] = ++$i;
                $Ccount=DB::select("select user_name from pt_partner where  id={$v['partner_id']}" );
                $Ccount=isset($Ccount[0]['user_name']) ? $Ccount[0]['user_name'] : 0;
                $data[$k]['pid']=$Ccount;
                $data[$k]['avatar'] = "<img src=\"{$v['avatar']}\" width=\"50px\" height=\"50px\" style=\"border-radius:50%;\">";
                $data[$k]['province'] = $v['province'] . "-" . $v['city'];
                if ($v['source_type'] == 0) {
                    $data[$k]['source_type'] = "D音";
                } else {

                }
            }
        }
        $re['data'] = $data;
        $re['total'] = $count;
        return $re;
    }


}